package tesis.util;

import tesis.dto.base.BaseRemoteOutBean;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.Response.Status;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.StringEntity;

public class Invoker {

    public static final String DEFAULT_CHARACTER_ENCODING = "UTF-8";

    protected static <T extends BaseRemoteOutBean> T getResultBean(String url, Class<?> type) {
        T resultBean = null;
        HttpResponse response;

        T result = null;
        try {
            result = (T) type.newInstance();
        } catch (Exception ex) {
            Logger.getLogger(Invoker.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            JAXBContext c = JAXBContext.newInstance(new Class[]{type});
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(url);

            response = httpclient.execute(httpget);
            if (response.getStatusLine().getStatusCode() == Status.OK.getStatusCode()) {
                HttpEntity resEntity = response.getEntity();

                if (resEntity != null) {
                    c = JAXBContext.newInstance(new Class[]{type});
                    Unmarshaller u = c.createUnmarshaller();
                    resultBean = (T) u.unmarshal(resEntity.getContent());
                }
                if (resEntity == null) {
                    resEntity.consumeContent();
                }
            } else {
                result.setError(true);
                result.setMensaje(response.getStatusLine().getReasonPhrase());
                return result;
            }
        } catch (Exception ex) {
            Logger.getLogger(Invoker.class.getName()).log(Level.SEVERE, null, ex);
            result.setError(true);
            result.setMensaje("Ocurrio un error en la invocacion");
            return result;
        }
        return resultBean;
    }

    protected static <T extends BaseRemoteOutBean> T getResultBean(String url, String xmlContent, Class<?> type) throws Exception {
        T resultBean = null;

        T result = null;
        try {
            result = (T) type.newInstance();
        } catch (Exception ex) {
            Logger.getLogger(Invoker.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            JAXBContext c = JAXBContext.newInstance(new Class[]{type});
            HttpClient client = new DefaultHttpClient();
            // Create POST method
            HttpPost method = new HttpPost(url);

            StringEntity input = new StringEntity(xmlContent, DEFAULT_CHARACTER_ENCODING);
            input.setContentType("application/xml");
            method.setEntity(input);

            HttpResponse response = client.execute(method);
            if (response.getStatusLine().getStatusCode() == Status.OK.getStatusCode()) {
                HttpEntity resEntity = response.getEntity();

                if (resEntity != null) {
                    c = JAXBContext.newInstance(new Class[]{type});
                    Unmarshaller u = c.createUnmarshaller();
                    resultBean = (T) u.unmarshal(resEntity.getContent());
                }
                if (resEntity == null) {
                    resEntity.consumeContent();
                }
            } else {
                result.setError(true);
                result.setMensaje(response.getStatusLine().getReasonPhrase());
                return result;
            }
        } catch (Exception ex) {
            Logger.getLogger(Invoker.class.getName()).log(Level.SEVERE, null, ex);
            result.setError(true);
            result.setMensaje("Ocurrio un error en la invocacion");
            return result;
        }
        return resultBean;
    }
}
